//
//  CarList.m
//  car
//
//  Created by chenxu on 15/3/18.
//  Copyright (c) 2015年 chenxu. All rights reserved.
//

#import "CarListView.h"
#import "Car_Cell.h"
#import "ODRefreshControl.h"
#import "NetAPIManager.h"
#import "Login.h"
#import "NSObject+ObjectMap.h"
#import "addcarViewController.h"
#define kCellIdentifier_carList @"Car_Cell"


@interface CarListView()
@property (nonatomic, strong) NSArray *cars;
@property (nonatomic , copy) CarSelectBlock selectblock;
@property (nonatomic , copy) CarModifyBlock modifyblock;
@property (nonatomic , copy) PushToAddCarViewBlock pushblock;

@property (nonatomic, strong) UITableView *myTableView;
@end

@implementation CarListView

#pragma TabBar
- (void)tabBarItemClicked{
    if (_myTableView.contentOffset.y > 0) {
        [_myTableView setContentOffset:CGPointZero animated:YES];
    }else{
        [self refresh];
    }
}

- (id)initWithFrame:(CGRect)frame cars:(NSArray *)cars selectblock:(CarSelectBlock)selectblock modifyblock:(CarModifyBlock)modifyblock pushblock:(PushToAddCarViewBlock)pushblock
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        _cars = cars;
        _selectblock = selectblock;
        _modifyblock = modifyblock;
        _pushblock = pushblock;
        _myTableView = ({
            UITableView *tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStyleGrouped];
            tableView.backgroundColor = kColorTableBG;
            tableView.delegate = self;
            tableView.dataSource = self;
            [tableView registerClass:[Car_Cell class] forCellReuseIdentifier:kCellIdentifier_carList];
            tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
            [self addSubview:tableView];
            tableView;
        });
        
        if (_cars.count > 0) {
            [_myTableView reloadData];
        }else{
            [self sendRequest];
        }
        _myTableView.tableFooterView = [self tableFooterView];

    }
    return self;
}

- (UIView*)tableFooterView{
    UIView *footerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 90)];
    UIButton *loginBtn = [UIButton buttonWithStyle:StrapThemeStyle andTitle:@"添加车辆" andFrame:CGRectMake(10, 0, kScreen_Width-30*2, 40) target:self action:@selector(pushToAddCarView)];
    [loginBtn setCenter:footerV.center];
    [footerV addSubview:loginBtn];
    return footerV;
}

- (void)pushToAddCarView{
   if(_pushblock){
        _pushblock();
    }
}

/*!
 *  @brief  刷新ui同时异步加载数据
 */
- (void)refreshUI{
    [_myTableView reloadData];
    [self refreshFirst];
}
- (void)refreshToQueryData{
    [self refresh];
}

- (void)refresh{
    [self sendRequest];
}

- (void)refreshFirst{
    if (_cars) {
        [self performSelector:@selector(refresh) withObject:nil afterDelay:0.3];
    }
}

- (void)sendRequest{
    if (_cars.count <= 0) {
      //  [self beginLoading];
    }
    __weak typeof(self) weakSelf = self;
    [[NetAPIManager sharedManager] request_cars:[[Login curLoginUser] tousernameParams] andBlock:^(id data, NSError *error) {
       // [self endLoading];
        if (data) {
            NSMutableArray* temp = [[NSMutableArray alloc]init];

            for (int i=0; i< [data count]; i++) {
                Car* car = [NSObject objectOfClass:@"Car" fromJSON:[data objectAtIndex:i]];
                [temp addObject:car];
            }
            //存入cars
            weakSelf.cars = temp;
            [weakSelf.myTableView reloadData];
        }
    }];
}

#pragma mark Table M
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.cars) {
        return [self.cars count];
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Car_Cell *cell = [[[NSBundle mainBundle] loadNibNamed:kCellIdentifier_carList owner:self options:nil] firstObject];
    
    [cell configWithCar:[self.cars objectAtIndex:indexPath.row]];
    if (_modifyblock){
        cell.buttonClicked = _modifyblock;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //UITableViewCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
  //  return cell.contentView.frame.size.height;
    return 76;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_selectblock) {
        Car* car =[self.cars objectAtIndex:indexPath.row];
        _selectblock(car);
       
    }
}




@end
